package com.he.controller;

import com.he.annotation.OperationLogger;
import com.he.domain.Result;
import com.he.domain.dto.AlbumDTO;
import com.he.domain.dto.ConditionDTO;
import com.he.domain.dto.PageResult;
import com.he.domain.vo.AlbumBackVO;
import com.he.domain.vo.AlbumVO;
import com.he.service.AlbumService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author He
 * @version 1.0
 * @Date 2023/10/22 11:29
 * @Desc 相册控制器
 */
@Tag(name = "相册管理", description = "相册管理")
@RestController
public class AlbumController {
    @Resource
    private AlbumService albumService;

    /**
     * 查看后台相册列表
     *
     * @param condition 条件
     * @return {@link PageResult<AlbumBackVO>} 后台相册列表
     */
    @Operation(description = "查看后台相册列表")
    @PreAuthorize("@ps.hasPermission('web:album:list')")
    @GetMapping("/admin/album/list")
    public Result<PageResult<AlbumBackVO>> listAlbumBackVO(ConditionDTO condition) {
        return Result.success(albumService.listAlbumBackVO(condition));
    }

    /**
     * 查看相册列表
     *
     * @return {@link Result<AlbumVO> 相册列表
     */
//    @VisitLogger(value = "相册")
    @Operation(description = "查看相册列表")
    @GetMapping("/album/list")
    public Result<List<AlbumVO>> listAlbumVO() {
        return Result.success(albumService.listAlbumVO());
    }

    /**
     * 添加相册
     *
     * @param album 相册信息
     * @return {@link Result<>}
     */
    @OperationLogger(value = "添加相册")
    @Operation(description = "添加相册")
    @PreAuthorize("@ps.hasPermission('web:album:add')")
    @PostMapping("/admin/album/add")
    public Result<?> addAlbum(@Validated @RequestBody AlbumDTO album) {
        albumService.addAlbum(album);
        return Result.success();
    }

    /**
     * 删除相册
     *
     * @param albumId 相册id
     * @return {@link Result}
     */
    @OperationLogger(value = "删除相册")
    @Operation(description = "删除相册")
    @PreAuthorize("@ps.hasPermission('web:album:delete')")
    @DeleteMapping("/admin/album/delete/{albumId}")
    public Result<?> deleteAlbum(@PathVariable("albumId") Integer albumId) {
        albumService.deleteAlbum(albumId);
        return Result.success();
    }

    /**
     * 上传相册封面
     *
     * @param file 文件
     * @return {@link Result<String>} 相册封面地址
     */
    @OperationLogger(value = "上传相册封面")
    @Operation(description = "上传相册封面")
    @PreAuthorize("@ps.hasPermission('web:album:add')")
    @PostMapping("/admin/album/upload")
    public Result<String> uploadAlbumCover(@RequestParam("file") MultipartFile file) {
        return Result.success(albumService.uploadAlbumCover(file));
    }

    /**
     * 编辑相册
     *
     * @param albumId 相册id
     * @return {@link Result<AlbumDTO>} 相册
     */
    @Operation(description = "编辑相册")
    @PreAuthorize("@ps.hasPermission('web:album:edit')")
    @GetMapping("/admin/album/edit/{albumId}")
    public Result<AlbumDTO> editAlbum(@PathVariable("albumId") Integer albumId) {
        return Result.success(albumService.editAlbum(albumId));
    }

    /**
     * 修改相册
     *
     * @param album 相册信息
     * @return {@link Result<>}
     */
    @OperationLogger(value = "修改相册")
    @Operation(description = "修改相册")
    @PreAuthorize("@ps.hasPermission('web:album:update')")
    @PutMapping("/admin/album/update")
    public Result<?> updateAlbum(@Validated @RequestBody AlbumDTO album) {
        albumService.updateAlbum(album);
        return Result.success();
    }
}
